package com.allwees.bs.module.fms.mq.listener;

import com.allwees.bs.core.modelbase.branch.EBranchType;
import com.allwees.bs.module.fms.entity.MerchantWithdrawAccount;
import com.allwees.bs.module.fms.req.MerchantWithdrawAccountInfoReq;
import com.allwees.bs.module.fms.service.MerchantAccountService;
import com.allwees.bs.module.fms.service.MerchantWithdrawAccountService;
import com.allwees.bs.module.mms.bo.MerchantCreatedBo;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author yangylsky
 * @since 2020-01-02
 */
@Component
@RabbitListener(queues = "q.Merchant.RetailerCreated")
public class CreateRetailerAccountListener {
    @Autowired
    private MerchantAccountService merchantAccountService;
    @Autowired
    private MerchantWithdrawAccountService merchantWithdrawAccountService;

    @RabbitHandler
    public void onEvent(MerchantCreatedBo bo) {
        merchantAccountService.createRetailerAccount(EBranchType.RETAILER, bo);

        MerchantWithdrawAccount account = new MerchantWithdrawAccount();
        account.setBranchType(EBranchType.RETAILER);
        account.setBankAccountNo(bo.getAccountNo());
        account.setBranchUuid(bo.getUuid());
        account.setBankCode(bo.getBankCode());
        account.setBankName(bo.getBankName());
        account.setBranchBank(bo.getBranchBank());
        account.setBranchBankLocation(bo.getBranchBankLocation());
        account.setIsDefault(true);
        account.setType(bo.getAccountType());
        account.setChannel(bo.getChannel());
        account.setIdNo(bo.getAccountOwnerIdNo());
        account.setName(bo.getAccountOwnerName());

        MerchantWithdrawAccountInfoReq req = new MerchantWithdrawAccountInfoReq();
        req.setBo(account);

        merchantWithdrawAccountService.createRetailerAccount(req);
    }
}
